<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<h1>promise</h1>
	</body>


	<script>
		// 请求一个图片 这里会错误
		let image = 'img/demo.jpg';

		fetch(image).then(response => {
			console.log(response)
			if (!response.ok) {
				throw new Error(`HTTP error! status: ${response.status}`);
			} else {
				return response.blob();
			}
		}).then(blob => {
			let url = URL.createObjectURL(blob);
			let img = document.createElement('img');

			img.src = url;

			document.body.appendChild(img);
		}).catch(error => {
			console.log(error)
		}).finally(() => {
			console.log('all is finally')
		})

		// 编写自己的promise
		let permise = new Promise((resolve, reject) => {
			setTimeout(function() {
				console.log('loading is over');
				resolve('success');
			}, 2000)
		})

		permise.then(res => {
			console.log(res);
		})
		console.log('all is over')
	</script>

</html>
